<template>
  <page-wrap
    :show-top="showTop"
    hide-tab
    :bg-color="uNavColor"
    :dark="true"
    back
    :title="pageTitle"
    :callBackUrl="gotoMyTabUrl"
    :switchTab="false"
  >
    <view :style="'padding-top:' + navHeight + 'px;'" v-if="key == '1'">
      <view class="password-input u-border-bottom">
        <input
          class="input"
          v-model="form.password"
          placeholder="请输入密码"
          placeholder-style="color: #bbbbbb;"
          type="text"
          :clearable="false"
          :adjust-position="false"
          label-width="0"
        />
      </view>
      <view style="margin: 32px 16px">
        <u-button shape="circle" type="primary" @click="handleBindCard"
          >提 交</u-button
        >
      </view>
    </view>
    <LoginDialog
      ref="loginDialog"
      @loginSuccessBack="loginSuccessBack"
    ></LoginDialog>
    <u-toast ref="uToast" />
  </page-wrap>
</template>

<script>
import { codeBinding, authCheck, getUserInfo } from '@/api/modules/career';
import LoginDialog from '@/components/LoginDialog';
export default {
  components: {
    LoginDialog,
  },
  data() {
    return {
      pageTitle: '激活职途卡',
      gotoMyTabUrl: '/pagesSub/userCenter/careerPathV2/index',
      uNavColor: this.$uNavColor,
      navHeight: getApp().globalData.navHeight,
      personId: uni.getStorageSync('person-id').slice(2) || undefined,
      form: {
        password: '',
      },
      disabled: false,
      loading: false,
      extra: '',
      key: '',
      opt: '',
    };
  },
  onLoad(opt) {
    if (opt.q) {
      console.log(opt);
      let careerCardParams = JSON.stringify(opt.q);
      uni.setStorageSync('careerCardParams', careerCardParams);
    }
  },
  created() {
    uni.setStorageSync('isZhituUser', true); //职途用户标识（注册的时候传regSource用
  },
  mounted() {
    if (!this.personId) {
      this.showLoginDialog();
    } else {
      this.getUserInfoFn();
    }
  },
  methods: {
    getUserInfoFn() {
      // 这个uid一定要传，因为在interceptors中，没有传参的请求的请求头不会带上uid，而这个请求一定要uid
      getUserInfo({ uid: '' }).then((res) => {
        if (res.code == 200 && res.data.cardNo) {
          uni.reLaunch({ url: '/pagesSub/userCenter/careerPathV2/index' });
        } else {
          const careerCardParams = JSON.parse(
            uni.getStorageSync('careerCardParams')
          );
          if (careerCardParams) {
            let url = decodeURIComponent(careerCardParams);
            if (url.indexOf('extra') > -1) {
              const queryParams = url.split('?')[1];
              this.extra = queryParams.split('&')['0'].replace('extra=', '');
              this.handleCheckAuth();
            }
          }
        }
      });
    },
    handleLogin() {
      if (!this.personId) {
        this.showLoginDialog();
      }
    },
    handleCheckAuth() {
      const params = {
        extra: this.extra,
      };
      authCheck(params).then((res) => {
        if (res.code == 200) {
          this.key = res.data;
          if (this.key == '0') {
            this.handleBindCard();
          }
        }
      });
    },
    handleBindCard() {
      uni.showLoading({
        title: '绑定中...',
      });
      const params = {
        codeKey: this.form.password,
        extra: this.extra,
      };
      codeBinding(params)
        .then((res) => {
          uni.hideLoading();
          if (res.code == 200) {
            // this.$refs.uToast.show({
            //   title: "职途卡绑定成功，请输入姓名和学号",
            //   duration: 1500,
            //   url: "/pagesSub/userCenter/careerPath/editUserInfo/index",
            //   params: {
            //     gotoCareerPathV2: "1",
            //   },
            // });
            this.$refs.uToast.show({
              title: '职途卡绑定成功!',
              duration: 1500,
            });

            setTimeout(() => {
              uni.reLaunch({ url: '/pagesSub/userCenter/careerPathV2/index' });
            }, 2000);
          } else if (
            res.code == 201 &&
            res.status_desc.indexOf('您已绑定了卡') > -1
          ) {
            uni.reLaunch({ url: '/pagesSub/userCenter/careerPathV2/index' });
          } else {
            this.$refs.uToast.show({
              title: res.status_desc,
              duration: 3000,
            });
          }
        })
        .catch((err) => {
          uni.hideLoading();
          this.$refs.uToast.show({
            title: err.status_desc,
            duration: 2000,
          });
        });
    },
    loginSuccessBack() {
      this.personId = uni.getStorageSync('person-id').slice(2) || undefined;
      this.getUserInfoFn();
    },
    showLoginDialog() {
      this.$refs.loginDialog.setVisibility(true);
    },
  },
};
</script>

<style lang="scss" scoped>
.password-input {
  margin: 60rpx 24rpx 0 24rpx;
  display: flex;
  align-items: center;
  padding: 24rpx 24rpx;
  // #ifdef H5
  padding: 24rpx 8rpx 20rpx;
  // #endif
  input {
    flex-grow: 1;
  }
  // #ifdef H5
  .input {
    flex-grow: 1;
    border: none;
    outline: none;
    font-family: inherit;
    line-height: 48rpx;
    font-size: $uni-font-size-base;
    &::placeholder {
      color: $uni-text-color-placeholder;
    }
  }
  // #endif
}
</style>
